package com.example.fishdriver.recyclerviewdemo;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
 * Created by linhonghong on 2015/11/19.
 */
public class PullToRefreshRecyclerViewUtil {
    /**
     * 找出最后一个可见的Positioon
     * @param layoutManager
     * @return
     */
    public int findLastVisibleItemPosition(RecyclerView.LayoutManager layoutManager){
        if(layoutManager != null) {

            if (layoutManager instanceof LinearLayoutManager) {
                return ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            }

            if (layoutManager instanceof GridLayoutManager) {
                return ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            }

        }
        return RecyclerView.NO_POSITION;
    }

    /**
     * 返回第一个完全可见视图的适配器位置
     * 这个位置并不包含适配器改变之后的视图
     * @param layoutManager
     * @return
     */
    public int findFirstCompletelyVisibleItemPosition(RecyclerView.LayoutManager layoutManager){
        if(layoutManager != null) {

            if (layoutManager instanceof LinearLayoutManager) {
                return ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
            }

            if (layoutManager instanceof GridLayoutManager) {
                return ((GridLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
            }

        }
        return RecyclerView.NO_POSITION;
    }

    public int findFirstVisibleItemPosition(RecyclerView.LayoutManager layoutManager){
        if(layoutManager != null) {

            if (layoutManager instanceof LinearLayoutManager) {
                return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
            }

            if (layoutManager instanceof GridLayoutManager) {
                return ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
            }

        }
        return RecyclerView.NO_POSITION;
    }
}
